package edu.hawaii.wicket.page.scheduler;
/**
 * A class that does Enums.
 * @author Gerolf Seitz
 * https://github.com/gseitz
 *@param <T> a type.
 */
public class EnumOption<T extends Enum<T>> extends Option<T> { 
  private static final long serialVersionUID = 1L;
  private final Mode mode;
/**
 * A method.
 */
  public enum Mode {
    /**
     * An Enum.
     */
    NAME, 
    /**
     * An Enum.
     */
    ORDINAL
  }
  /**
  * A method.
  * @param name a name
  * @param defaultValue a value
  * @param mode a mode
  */
  public EnumOption(String name, T defaultValue, Mode mode) { 
    super(name, defaultValue);
    this.mode = mode;
  } 
  /**
   * A method.
   * @param name a name
   * @param defaultValue a value
   */
  public EnumOption(String name, T defaultValue) { 
    this(name, defaultValue, Mode.ORDINAL);
  } 
 
  /**
   * Call to super for equals, necessary for findbugs to work.
   * @param obj object to compare.
   * @return true if equal, false otherwise.
   */
  @Override
  //necessary...VERY necessary
  @SuppressWarnings("PMD.CollapsibleIfStatements")
  public boolean equals(Object obj) { 
    if (obj != null) { 
      if (obj.getClass() == getClass()) { 

        return super.equals(obj);
      }
    }
    return false;
  }
  
  /**
   * Call to super for hashCode, necessary for checkstyle to work.
   * @return hash code for this obj.
   */
  @Override
  public int hashCode() { 
    if (super.hashCode() != 0) { 
      return super.hashCode();
    }
    return 0;
  }
  
  @Override
  /**
   * A method.
   */
  public String asJsonString() {  
    StringBuilder sb = new StringBuilder();
    sb.append(getName());
    sb.append(":");
    if (mode.equals(Mode.NAME)) { 
      sb.append("'");
      sb.append(getValue().name());
      sb.append("'");
    } 
    else { 
      sb.append(getValue().ordinal());
    }
    return sb.toString();
  } 

} 